using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class XmlTextWriterPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         
        try
        {
            string fileName = Server.MapPath("XmlTextWriter.xml");
            XmlTextWriter xtw = new XmlTextWriter(fileName, Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;
            xtw.Indentation = 3;
            xtw.WriteStartDocument();
            xtw.WriteComment(
                "Written by Pham Huu Khang " 
                + DateTime.Now.ToLongDateString());
            xtw.WriteStartElement("root");
            for(int i=1; i<3; i++)
            {
             
                xtw.WriteStartElement("Item" );
                xtw.WriteAttributeString("ID", "ItemID" + i.ToString());            
                xtw.WriteElementString("Name", "ItemName" + i.ToString());
                xtw.WriteElementString("Unit", "Unit" + i.ToString());
                xtw.WriteElementString("Price", i.ToString());
                xtw.WriteEndElement();
            }
            xtw.WriteEndElement();
            xtw.WriteEndDocument();
             
            xtw.Flush();         
            xtw.Close();
        }
        catch(Exception ex)
        {
            Literal1.Text = ex.Message;
        }
    }

    
        
}
